package top.xiesen.testepolymorphic;
/**
 * 多态测试
 * 多态存在的必要条件：有继承、有方法重写、父类引用指向子类对象
 * @author Allen
 *
 */
public class Test {

	//多态 	编译时类型Animal
	public static void testAnimalVoice(Animal c){
		c.voice();
		// instanceof:判断一个对象是否是这个类型的对象
		if(c instanceof Cat){
			((Cat) c).CatchaMouse();
		}
	}
	
	/*public static void testAnimalVoice(Dog d){
		d.voice();
	}
	public static void testAnimalVoice(Pig p){
		p.voice();
	}*/
	public static void main(String[] args) {
		//运行时类型 Cat
		Cat c = new Cat();
		Animal d = new Dog();	//父类引用指向子类对象
		Animal t = new Tigger();
		testAnimalVoice(c);
		testAnimalVoice(d);
		testAnimalVoice(t);
		
		Dog d2 = (Dog) d;//强制转型
		d2.seeDoor();
		
	}
}
